var $ = require('../../../utils/helper.js'), that = null
var app = getApp()
Page({
  data: {
    info: {
      type: 1
    },
    areaindex:[0,0],
    areaindex_company:0
  },
  onLoad(e) {
    that = this
  },
  onShow: function () {
  
  },
  onReady: function () {
    $.check(that)
    area_list:[[],[]]
  },
  init: function () {
    $.post("api/auth/get", null,function (data) {
      that.init_core(data)
      
    }, function (message) {
      that.init_core()
    })

  },

  init_core: function (data){
    if (data != null) {
      that.setData({ info: data })
    }
    $.post("api/index/getarea", null, function (list) {
      that.setData({ ['area_list[0]']: list })
      if (that.data.info != null && that.data.info.type == 1 && that.data.info.status != null) {

        let personal_area_id = that.data.info.personal_area_id
        for (var i in list) {
          if (list[i].id == data.presonal_area_parent_id) {
            that.setData({ ['areaindex[0]']: i })
          }
        }
        that.setcitylist_core(data.presonal_area_parent_id, null, function (sublist) {
          for (var j in sublist) {
            if (sublist[j].id == personal_area_id) {
              that.setData({ ['areaindex[1]']: j })
            }
          }
        })
      } else {
        that.setcitylist_core(1, null)
      }
      if (that.data.info != null && that.data.info.type == 2 && that.data.info.status != null) {

        let company_area_id = that.data.info.company_area_id
        for (var i in list) {
          if (list[i].id == data.company_area_id) {
            that.setData({ ['areaindex_company']: i })
          }
        }
      }
    })
  },
  changetype:function(e){
    that.setData({ ["info.type"]: e.detail.value})
  },
  setinput:function(e){
    that.setData({ ["info." + e.currentTarget.dataset.name]: e.detail.value })
    
  },
  upload:function(e){
    console.log(e)
    wx.chooseImage({
      success(res) {
        let tempFilePaths = res.tempFilePaths

        $.upload("auth", tempFilePaths[0], function (data) {
          that.setData({
            ["info." + e.currentTarget.dataset.name]: data
          })
        })
      }
    })
  },
  save:function(){
    $.post("api/auth/set",that.data.info,function(){
      wx.navigateBack({
        
      })
    })
  },
  setcitylist:function(e){
    console.log(e)
    if (e.detail.column != 0) {
      return
    }
    let id = that.data.area_list[0][e.detail.value].id
  
    that.setcitylist_core(id, e)
  },
  setcitylist_core:function(id,e,cb){
    $.post("api/index/getarea", {
      parent_id: id
    }, function (list) {
      that.setData({
        "area_list[1]": list
      })
      if (e != null) {
        that.setData({
          "areaindex[0]": e.detail.value,
          "areaindex[1]": 0
        })
      }
      console.log(that.data.area_list)
      if (cb != null) {
        cb(list)
      }
    })
  }
  , setarea_personal: function (e) {
    that.setData({
      "areaindex[0]": e.detail.value[0],
      "areaindex[1]": e.detail.value[1]
    })
    let id = that.data.area_list[1][that.data.areaindex[1]].id
    that.setData({ ['info.personal_area_id']:id})
  },
  setarea_company:function(e){
    that.setData({
      "areaindex_company": e.detail.value
    })
    let id = that.data.area_list[0][that.data.areaindex_company].id
    that.setData({ ['info.company_area_id']: id })
    console.log(that.data.info)
  }
}) 